package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Appointment;
import cn.wolfcode.mapper.AppointmentMapper;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {
    private AppointmentMapper appointmentMapper ;

    @Autowired
    public void setAppointmentMapper(AppointmentMapper appointmentMapper) {
        this.appointmentMapper = appointmentMapper;
    }

    @Override
    public void insert(Appointment appointment) {

        String date = new SimpleDateFormat("yyyyMMddHHss").format(new Date());
        int random = new Random().nextInt(90000)+10000;
        appointment.setAno(date+random);

        appointment.setCreateTime(new Date());
        appointmentMapper.insert(appointment);
    }

    @Override
    public void delete(Long id) {
        appointmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Appointment appointment) {
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public Appointment selectById(Long id) {
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        return appointment ;
    }

    @Override
    public List<Appointment> selectAll() {
        List<Appointment> appointments = appointmentMapper.selectAll();
        return appointments ;
    }

    @Override
    public PageInfo<Appointment> query(AppointmentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"a.create_time desc");
        List<Appointment> appointments = appointmentMapper.queryAll(qo);
        return new PageInfo<>(appointments);
    }

    @Override
    public void updateStatus(Long id, int status) {
        appointmentMapper.updateStatus(id,status);
    }

    @Override
    public Appointment get(Long id) {
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        return appointment;
    }
}
